package com.hanbang.gycatering.comeon;

import org.json.JSONException;
import org.json.JSONObject;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.hanbang.gycatering.utils.ShareMethod;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ForgetPasswordActivity extends BaseActivity {
	private EditText userphone, authCode, password1, password2;
	private TextView title;
	private TextView authCodeButton = null;
	// 验证码按钮倒计时 总时长和间隔时长
	private long totalTime = 60000, intervalTime = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forget_password);
		((TextView) findViewById(R.id.top_bar_title)).setText("密码修改");
		setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
		title = (TextView) findViewById(R.id.user_register_again_password_title);
		// 发送验证码按钮
		authCodeButton = (TextView) findViewById(R.id.user_forget_password_auth_code_button);
		authCodeButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String phone = userphone.getText().toString().trim();
				title.setVisibility(View.VISIBLE);
				if (phone.equals("")) {
					title.setText("请输入手机号！");
					return;
				} else if (!phone.matches("^[1][3,5,7,8]+\\d{9}")) {
					title.setText("请输入正确的手机号！");
					return;
				}
				title.setVisibility(View.INVISIBLE);
				getCode(phone,2);
			}
		});

		userphone = ((EditText) findViewById(R.id.user_forget_password_phone));
		authCode = (EditText) findViewById(R.id.user_forget_password_auth_code_et);
		password1 = ((EditText) findViewById(R.id.user_forget_password_password1));
		password2 = ((EditText) findViewById(R.id.user_forget_password_password2));

		findViewById(R.id.user_forget_password_button).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View view) {
						String t2 = userphone.getText().toString().trim();
						String t3 = password1.getText().toString().trim();
						String t4 = password2.getText().toString().trim();
						String t5 = authCode.getText().toString().trim();

						title.setVisibility(View.VISIBLE);
						title.setText("");
						if (t2.equals("")) {
							title.setText("请输入手机号！");
							return;
						} else if (!t2.matches("^[1][3,5,7,8]+\\d{9}")) {
							title.setText("请输入正确的手机号！");
							return;
						} else if (t5 == null || t5.equals("")
								|| t5.length() != 6) {
							title.setText("请输入6位数验证码！");
							return;
						} else if (t3.equals("") || t4.equals("")) {
							title.setText("请输入密码！");
							return;
						} else if (t3.length() < 6 || t4.length() < 6) {
							title.setText("密码长度不足6位");
							return;
						} else if (!t3.equals(t4)) {
							title.setText("两次输入的密码不一致！");
							return;
						}
						title.setVisibility(View.INVISIBLE);
						postService(t2,t3,t4,t5);
					}
				});
	}
	private void postService(final String userphone,
			final String password1, final String password2,final String code ) {
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.findpasswod  + "&userphone=" + userphone + "&password="
						+ password1 + "&password2=" + password2+"&code="+code ,
				new MyRequestCallBack<String>(ForgetPasswordActivity.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								MyLog.e("postService",
										HttpInterfaces.registeruser
												+ "&userphone=" + userphone
												+ "&password=" + password1
												+ "&password2=" + password2+"&code="+code );
								Intent intent = new Intent(
										ForgetPasswordActivity.this,
										LoginActivity.class);
								intent.putExtra("userphone", userphone);
								intent.putExtra("password", password1);
								setResult(999, intent);
								finish();
							} else {
								MyToast.show(ForgetPasswordActivity.this, "修改失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(ForgetPasswordActivity.this, "修改失败",
									Toast.LENGTH_SHORT);
						}
					}
				});
	}
 
		 //获取验证码
		 private void getCode(String phone,int type) {
			 ((MyApplication) getApplicationContext()).httpUtils.send(
						HttpMethod.GET, HttpInterfaces.code + "&userphone="
								+ phone + "&type="+ type ,
						new MyRequestCallBack<String>(ForgetPasswordActivity.this,
								getString(R.string.loadding)) {
							@Override
							public void onSuccess(ResponseInfo<String> responseInfo) {
								super.onSuccess(responseInfo);
								try {
									JSONObject json = new JSONObject(
											responseInfo.result);
									if (json.getInt("state") == 1) {
										countDownTimer.start(); 
									} else {
										MyToast.show(ForgetPasswordActivity.this, json.getString("erro"),
												Toast.LENGTH_SHORT);
									}
								} catch (JSONException e) {
									e.printStackTrace();
									MyToast.show(ForgetPasswordActivity.this, "注册失败",
											Toast.LENGTH_SHORT);
								}
							}
						});
		 
	}
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	CountDownTimer countDownTimer = new CountDownTimer(totalTime, intervalTime) {
		@Override
		public void onFinish() {// 计时完毕时触发
			authCodeButton.setText("重新发送");
			authCodeButton.setEnabled(true);
			// authCodeButton.setClickable(true);
		}

		@Override
		public void onTick(long currentTime) {// 计时过程显示
			// authCodeButton.setClickable(false);
			authCodeButton.setEnabled(false);
			authCodeButton.setText(currentTime / 1000 + "秒");
		}
	};
}
